package com.example.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.school.dto.AdminGetListDto;
import com.example.school.dto.AdminSaveStudentDto;
import com.example.school.dto.AdminUpdateStudentDto;
import com.example.school.pojo.Student;
import com.example.school.service.StudentService;
import com.example.school.mapper.StudentMapper;
import com.example.school.utils.common.enums.Code;
import com.example.school.utils.common.result.PageResponseResult;
import com.example.school.utils.common.result.Result;
import com.example.school.utils.config.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 24354
* @description 针对表【student】的数据库操作Service实现
* @createDate 2024-03-16 23:14:27
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Result getStudentList(AdminGetListDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        dto.checkParam();
        IPage page=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Student> wrapper=new LambdaQueryWrapper<>();
        if (dto.getMimo()!=null&&!dto.getMimo().equals("")) {
            wrapper.like(Student::getStudentName,dto.getMimo());
            wrapper.or().like(Student::getStudentPhone,dto.getMimo());
            wrapper.or().like(Student::getStudentNumber,dto.getMimo());
            wrapper.or().like(Student::getStudentClass,dto.getMimo());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return Result.okResult(result);
    }

    @Override
    public Result saveStudent(AdminSaveStudentDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Student checkStudent = studentMapper.selectOne(Wrappers.<Student>lambdaQuery()
                .eq(Student::getStudentName, dto.getName())
                .or().eq(Student::getStudentNumber, dto.getNumber())
                .or().eq(Student::getStudentPhone, dto.getPhone()));
        if (checkStudent!=null) {
            return Result.errorResult(Code.DATA_EXIST);
        }
        Student student=new Student();
        student.setStudentName(dto.getName());
        student.setStudentNumber(dto.getNumber());
        student.setStudentClass(dto.getStudentClass());
        student.setStudentPhone(dto.getPhone());
        student.setGrade(dto.getGrade());
        int checkSave = studentMapper.insert(student);
        if (checkSave==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result removeStudent(Long id) {
        if (id==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Student checkStudent = studentMapper.selectById(id);
        if (checkStudent==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        int checkRemove = studentMapper.deleteById(id);
        if (checkRemove==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result updateStudent(AdminUpdateStudentDto dto) {

        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Student checkStudent = studentMapper.selectById(dto.getStudentId());
        if (checkStudent==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Student student=new Student();
        BeanUtils.copyProperties(checkStudent,student);
        if (dto.getStudentName()!=null&&!dto.getStudentName().equals("")) {
            student.setStudentName(dto.getStudentName());
        }
        if (dto.getStudentNumber()!=null) {
            student.setStudentNumber(dto.getStudentNumber());
        }
        if (dto.getStudentClass()!=null&&!dto.getStudentClass().equals("")) {
            student.setStudentClass(dto.getStudentClass());
        }
        if (dto.getGrade()!=null&&!dto.getGrade().equals("")&&dto.getGrade().length()!=0) {
            student.setGrade(dto.getGrade());
        }
        if (dto.getStudentPhone()!=null) {
            student.setStudentPhone(dto.getStudentPhone());
        }
        int checkUpdate = studentMapper.updateById(student);
        if (checkUpdate==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }
}




